package com.brkj.course;

import android.content.Context;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.brkj.download.DownLoadCourseActivity;
import com.brkj.download.DownloadingCourseActivity;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.model.DS_Course;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.PullListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dgl on 2014/4/25.
 */
public class CourseViewOffline {

    private ViewPager mPager;// 页卡内容
    private List<View> listViews; // Tab页面列表
    private ImageView cursor;// 动画图片
    private TextView t1, t2, t3;// 页卡头标
    private ImageView t1Icon,t2Icon;
    private int offset = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度

    private DownLoadCourseActivity downLoadCourse;
    private DownloadingCourseActivity downloadingCourse;
    private boolean isEdit =false;
    private View view;

    private Context mContext;
    private boolean hasShown = false;

    public CourseViewOffline(Context context){
        mContext = context;
    }

    public View getView() {
        view =  LayoutInflater.from(mContext).inflate(R.layout.download_course_main, null);




        return view;
    }

    public void show(){
        if(hasShown){
            return;
        }else{
            InitImageView();
            InitTextView();
            InitViewPager();
            InitEdit();
            hasShown = true;
        }

    }

    private void InitEdit() {
//        setRightBtn("编辑", new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                isEdit = !isEdit;
//                if(isEdit){
//                    setRightBtnText("取消");
//                }else{
//                    setRightBtnText("编辑");
//                }
//                downloadingCourse.setIsEdit(isEdit);
//                downLoadCourse.setIsEdit(isEdit);
//
//            }
//        });

    }


    /**
     * 初始化头标
     */
    private void InitTextView() {
        t1 = (TextView) view.findViewById(R.id.text1);
        t2 = (TextView) view.findViewById(R.id.text2);

        t1.setOnClickListener(new MyOnClickListener(0));
        t2.setOnClickListener(new MyOnClickListener(1));

//		t1.setTextColor(getResources().getColor(R.color.brkj_blue));
    }

    /**
     * 初始化ViewPager
     */
    private void InitViewPager() {
        mPager = (ViewPager) view.findViewById(R.id.vPager);
        listViews = new ArrayList<View>();
        LayoutInflater mInflater = ((BaseCoreActivity)mContext).getLayoutInflater();
        listViews.add(mInflater.inflate(R.layout.download_course, null));
        listViews.add(mInflater.inflate(R.layout.download_course, null));
        mPager.setAdapter(new MyPagerAdapter(listViews));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * 初始化动画
     */
    private void InitImageView() {
        cursor = (ImageView) view.findViewById(R.id.cursor);
//		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.chat_tab_a)
//				.getWidth();// 获取图片宽度
        DisplayMetrics dm = new DisplayMetrics();
        ((BaseCoreActivity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        cursor.setImageMatrix(matrix);// 设置动画初始位置
    }

    /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }


        @Override
        public Object instantiateItem(View arg0, int arg1) {

            if (arg1 < 3) {
                ((ViewPager) arg0).addView(mListViews.get(arg1 % 3), 0);
            }

            if (arg1 == 0) {
                downLoadCourse = new DownLoadCourseActivity(arg0, mContext);
                downLoadCourse.Start(t1);
            }
            if (arg1 == 1) {
                downloadingCourse = new DownloadingCourseActivity(arg0, mContext);
                downloadingCourse.Start(t2);
            }
            return mListViews.get(arg1 % 2);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
//		int two = one * 2;// 页卡1 -> 页卡3 偏移量

        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(one, 0, 0, 0);
                    }

                    currIndex = arg0;
                    animation.setFillAfter(true);// True:图片停在动画结束位置
                    animation.setDuration(300);
                    cursor.startAnimation(animation);

//				t1.setTextColor(getResources().getColor(R.color.brkj_blue));
//
//				t2.setTextColor(getResources().getColor(R.color.Text_gray));


                    break;
                case 1:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, one, 0, 0);
                    }

                    currIndex = arg0;
                    animation.setFillAfter(true);// True:图片停在动画结束位置
                    animation.setDuration(300);
                    cursor.startAnimation(animation);

//				t1.setTextColor(getResources().getColor(R.color.Text_gray));
//
//				t2.setTextColor(getResources().getColor(R.color.brkj_blue));

                    break;
            }

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        public void onPageScrollStateChanged(int arg0) {
        }
    }


    public void Back(View v) {
        downLoadCourse.Stop();
        downloadingCourse.Stop();
    }

    public void DeleteAllCourse(View v){

//		DownLoadCourseManager CrMng = new DownLoadCourseManager(this);
//		CrMng.setExceptFile(downloadingCourse.DownloadingtFileList);
//		CrMng.DeleteAllCourse();

    }

}
